//反向评价
//杨帆
interface CarReverseCommentManagent extends ng.IScope{ //IBaseViewModel<IReverseCommentQueryTerms>{
    /**
     * 选中好评，一般，差评中的哪一个
     */
    selectItem:string;

    /**
     * 好评的切换
     */
    goodIf:boolean;
    /**
     * 一般的切换
     */
    commentIf:boolean;
    /**
     * 差评的切换
     */
    badIf:boolean;
   

    /**
     * 好评
     */
    goodEvaluation:()=>void;
    /**
     * 一般
     */
    commenEvaluation:()=>void;
    /**
     * 差评
     */
    badEvaluation:()=>void;


    /**
     * 评价数据
     */
    goodArrList:ReverseEvaluateListItemResponse[];
    /**
     * 一般数据
     */
    commentArrList:ReverseEvaluateListItemResponse[];
     /**
     * 差评数据
     */
    badArrList:ReverseEvaluateListItemResponse[];

    /**
     * 跳转到发货单详情
     */
    changePath:(item:Igoodvaluation)=>void;


    totalItems:number;
    currentPages:number;

    pageChanged:(page: number)=>void;
    total:number;
    max:number

    /**
     * 只读，禁用
     */
    isReadonly:boolean;


    /**
     * 11.16 查询条件
     */
    queryTerms:IReverseCommentQueryTerms
    /**
     * 客服专员下拉
     */
    customerServiceDropDown:EmployeeListItemResponse[];
    /**
     * 调度专员下拉
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 查询事件
     */
    query(selectEva):void;
    /**
     * 查询状态
     */
    querying:boolean

}

/**
 * 好评数据
 */
interface Igoodvaluation {
    //序号
    index?:number;
    name:string;
    date:string;
    grade:number;
    //评分下的标签
    tap:Array<any>;
    //评价内容
    content:string  
}

//查询条件
interface IReverseCommentQueryTerms {
    //
    ratingType:string;
    //调度专员
    dispatchOfficerId :string;
    //客服专员
    customerServiceOfficerId:string;
    //合同号
    contractNum:string;
    //评价起始时间
    commentStartTime:string;
    //评价结束时间
    commentEndTime:string
}

class CarReverseCommentManagentController{// extends ListController<CarReverseCommentManagent,IReverseCommentQueryTerms>
    appraiserId:string;
    appraiser:string;
    skip:number = 0;
    pages:number=0;

    
    
    constructor(private $scope:CarReverseCommentManagent,private carService:ICarService,private $location: ng.ILocationService ,private $timeout:ng.ITimeoutService,private orderService:IorderService,private $ngBootbox:BootboxService,
        private $state: angular.ui.IStateService,private feeService: IFeeService,private employeeService:IEmployeeService){
        this.appraiserId = JSON.parse(localStorage.getItem("loginData")).id;//评价人编号
        this.appraiser = JSON.parse(localStorage.getItem("loginData")).realName;//评论
        
        this.init();
    }

    init():void{
        this.$scope.max=5;
        this.$scope.totalItems= 5;

        this.$scope.isReadonly = true;
        // this.$scope.currentPage = JSON.parse(window.localStorage.getItem('commentPage')).page

        $('#openStartTime').click(function () {
        $('#commentStartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#commentEndTime').datetimepicker('show');
        });
        //初始化查询条件
        this.$scope.queryTerms = {
            ratingType:"",
            dispatchOfficerId:"",
            customerServiceOfficerId:"",
            contractNum:"",
            commentStartTime:"",
            commentEndTime:""
        };
        //客服专员下拉
        this.loadCustomerServiceDropDown();
        //调度专员下拉
        this.loadDispatchOfficerDropDown();

        //存储查询条件
        if(window.localStorage.getItem('carReverseCommentQuery')){
            this.$scope.queryTerms = {
                ratingType:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.ratingType,
                dispatchOfficerId:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.dispatchOfficerId,
                customerServiceOfficerId:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.customerServiceOfficerId,
                contractNum:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.contractNum,
                commentStartTime:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.commentStartTime,
                commentEndTime:JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.commentEndTime
            }
        }
        

        //一般
        this.$scope.commenEvaluation= this.commenEvaluation;
        //好评
        this.$scope.goodEvaluation = this.goodEvaluation;
        //差评
        this.$scope.badEvaluation = this.badEvaluation;

        //跳转发货单详情
        this.$scope.changePath = this.changePath;

        this.$scope.pageChanged = this.pageChanged;

        this.$scope.query=this.query;

        
        console.log('index',window.localStorage.getItem('selectItem'))
        if(this.$location.search().name){
            if(window.localStorage.getItem('selectItem')=="2"){
                this.commenEvaluation()
                
            }else if(window.localStorage.getItem('selectItem') == '3'){
                this.badEvaluation();
  
            }else{
                this.goodEvaluation()
            }
            // window.localStorage.removeItem('selectItem');
            // window.localStorage.removeItem('commentPage');
        }else{
            this.$scope.goodIf = true;
            this.$scope.commentIf = false;
            this.$scope.badIf = false;

            var pageInfo=JSON.parse(window.localStorage.getItem('commentPage'));
            this.$scope.currentPages=(pageInfo&&pageInfo.page)|1;
            //获取好评
            this.load('1',this.$scope.queryTerms.dispatchOfficerId,this.$scope.queryTerms.customerServiceOfficerId,this.$scope.queryTerms.contractNum,
            this.$scope.queryTerms.commentStartTime,this.$scope.queryTerms.commentEndTime,(this.$scope.currentPages-1)*5,5)
        }

    }

    //加载数据
    load(ratingType:string,dispatchOfficerId:string,customerServiceOfficerId:string,contractNum:string,commentStartTime:string,commentEndTime:string,skip:number,count:number,page?:number):void{
        this.$scope.querying=true;
        this.carService.reverseEvaluateList(ratingType,dispatchOfficerId,customerServiceOfficerId,contractNum,commentStartTime,commentEndTime,skip,count).then((res)=>{
                this.$scope.total = res.data.total;
                this.$scope.goodArrList = res.data.data;
                this.$scope.commentArrList = res.data.data;
                this.$scope.badArrList = res.data.data;
                console.log('d',this.$scope.commentArrList)
                if(!page){page = 0}else{
                    page = page -1;
                }
                res.data.data.forEach((item,index)=>{
                    item.index=(index + 1 + skip).toString()
                    this.$scope.goodArrList[index]['rate'] = item.ratingOptions.split("，");
                    this.$scope.commentArrList[index]['rate'] = item.ratingOptions.split("，")
                    this.$scope.badArrList[index]['rate'] = item.ratingOptions.split("，")
                })
                this.$scope.totalItems = res.data.total;

                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    query=(selectEva:string)=>{
        if(this.$scope.selectItem == '2'){
            selectEva="2";
        }else if(this.$scope.selectItem == '3'){
            selectEva="3";
        }else{
            selectEva="1";
        }

        this.load(selectEva,this.$scope.queryTerms.dispatchOfficerId,this.$scope.queryTerms.customerServiceOfficerId,this.$scope.queryTerms.contractNum,
            this.$scope.queryTerms.commentStartTime,this.$scope.queryTerms.commentEndTime,(this.$scope.currentPages-1)*5,5)

        var carReverseCommentQuery={
            selectEva:selectEva,
            dispatchOfficerId:this.$scope.queryTerms.dispatchOfficerId,
            customerServiceOfficerId:this.$scope.queryTerms.customerServiceOfficerId,
            contractNum:this.$scope.queryTerms.contractNum,
            commentStartTime:this.$scope.queryTerms.commentStartTime,
            commentEndTime:this.$scope.queryTerms.commentEndTime
        }
        window.localStorage.setItem('carReverseCommentQuery',JSON.stringify({carReverseCommentQuery}));
        
    }


    // load(ratingType:string,skip:number,count:number,page?:number):void{
    //     this.carService.reverseEvaluateList(ratingType,skip,count).then((res)=>{//好评
    //         this.$scope.total = res.data.total;
    //         this.$scope.goodArrList = res.data.data;
    //         this.$scope.commentArrList = res.data.data;
    //         this.$scope.badArrList = res.data.data;
    //         console.log('d',this.$scope.commentArrList)
    //         if(!page){page = 0}else{
    //             page = page -1;
    //         }
    //         res.data.data.forEach((item,index)=>{
    //             item.index=(index + 1 + skip).toString()
    //             this.$scope.goodArrList[index]['rate'] = item.ratingOptions.split("，");
    //             this.$scope.commentArrList[index]['rate'] = item.ratingOptions.split("，")
    //             this.$scope.badArrList[index]['rate'] = item.ratingOptions.split("，")
    //         })
    //         this.$scope.totalItems = res.data.total;
    //     });
    // }

    /**
     * 好评
     */
    goodEvaluation=()=>{
        $("#goodEvaluation").addClass("Evaluation");
        $("#commenEvaluation").removeClass("Evaluation")
        $('#haveEvaluation').removeClass('Evaluation')

        this.$scope.goodIf = true;
        this.$scope.commentIf = false;
        this.$scope.badIf = false;
        if(this.$location.search().name && window.localStorage.getItem('selectItem')=="1"){
            var commentPageInfo = JSON.parse(window.localStorage.getItem('commentPage'));    
            this.$scope.currentPages=(commentPageInfo&&commentPageInfo.page)||1;
        }else{
            this.$scope.currentPages = 1 ;
        }
        
        this.pageChanged(this.$scope.currentPages);
        this.$scope.selectItem = '1';
        window.localStorage.setItem('selectItem','1')
    }

    /**
     * 一般
     */
    commenEvaluation=()=>{
        $("#goodEvaluation").removeClass("Evaluation");
        $("#commenEvaluation").addClass("Evaluation");
        $("#haveEvaluation").removeClass("Evaluation");

        this.$scope.goodIf = false;
        this.$scope.commentIf = true;
        this.$scope.badIf = false;

        if(this.$location.search().name && window.localStorage.getItem('selectItem')=="2"){
            var commentPageInfo = JSON.parse(window.localStorage.getItem('commentPage'));    
            this.$scope.currentPages=(commentPageInfo&&commentPageInfo.page)||1;
        }else{
            this.$scope.currentPages = 1 ;
        }

        this.pageChanged(this.$scope.currentPages);
        this.$scope.selectItem = '2';
        window.localStorage.setItem('selectItem','2')
       
    }

    /**
     * 差评
     */
    badEvaluation=()=>{
        $("#goodEvaluation").removeClass("Evaluation");
        $("#commenEvaluation").removeClass("Evaluation");
        $("#haveEvaluation").addClass("Evaluation");

        this.$scope.goodIf = false;
        this.$scope.commentIf = false;
        this.$scope.badIf = true;
        if(this.$location.search().name && window.localStorage.getItem('selectItem')=="3"){
            var commentPageInfo = JSON.parse(window.localStorage.getItem('commentPage'));    
            this.$scope.currentPages=(commentPageInfo&&commentPageInfo.page)||1;
        }else{
            this.$scope.currentPages = 1 ;
        }
        this.pageChanged(this.$scope.currentPages);
        this.$scope.selectItem = '3';
        window.localStorage.setItem('selectItem','3');
    }


    /**
     * 分页
     */
    pageChanged=(page:number)=>{
        console.log('paging chaning');
        this.$scope.currentPages=page;

        var skip = (page -1 )* this.$scope.max ;

        if(window.localStorage.getItem('carReverseCommentQuery')){
            // console.log('dispatchOfficerId',JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.dispatchOfficerId)
            var dispatchOfficerId=JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.dispatchOfficerId,
            customerServiceOfficerId=JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.customerServiceOfficerId,
            contractNum = JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.contractNum,
            commentStartTime = JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.commentStartTime,
            commentEndTime = JSON.parse(window.localStorage.getItem('carReverseCommentQuery')).carReverseCommentQuery.commentEndTime

            if(this.$scope.goodIf){
                this.load('1',dispatchOfficerId,customerServiceOfficerId,contractNum,commentStartTime,commentEndTime,skip,5,page);
            }
            if(this.$scope.commentIf){
                this.load('2',dispatchOfficerId,customerServiceOfficerId,contractNum,commentStartTime,commentEndTime,skip,5,page);
            }
            if(this.$scope.badIf){
                this.load('3',dispatchOfficerId,customerServiceOfficerId,contractNum,commentStartTime,commentEndTime,skip,5,page);
            }
        }else{
            if(this.$scope.goodIf){
                this.load('1',"","","","","",skip,5,page);
            }
            if(this.$scope.commentIf){
                this.load('2',"","","","","",skip,5,page);
            }
            if(this.$scope.badIf){
                this.load('3',"","","","","",skip,5,page);
            }
        }

        window.localStorage.setItem('commentPage',JSON.stringify({skip:skip,page:page}))

        console.log('page',JSON.parse(window.localStorage.getItem('commentPage')).page)
        console.log('skip',JSON.parse(window.localStorage.getItem('commentPage')).skip)
    }

    /**
     * 跳转发货单详情
     */
    changePath=(item)=>{
        // console.log('item',item)
        // item.carrierOrderId='4b935f84-590a-4f8e-a6b2-e13572ada62e'
            this.orderService.carryOrder(item.carrierOrderId).then(res => {
                if (res.data.orderList.length > 1) {
                    this.$state.go('app.order.LTL_detail',{ id:item.carrierOrderId ,  name:'reverseComment',isReverse:true });//, type:"货已送达"
                    
                } else {    
                    this.$state.go('app.order.invoice_detail',{ id:item.carrierOrderId ,  name:'reverseComment',isReverse:true });
                } 
            })
        
    }

    /**
     * 客服专员下拉
     */
    loadCustomerServiceDropDown():void{
        this.employeeService.getbytype("4",0,-1).then(result => {//CommissionerType.CustomerService.toString()
            this.$scope.customerServiceDropDown = result.data.data;
        });
    }
    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown():void{
        this.employeeService.getbytype("2",0,-1).then(result => {//CommissionerType.Dispatch.toString()
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }


}

angular.module("tms").controller("carReverseCommentController", ["$scope", "carService","$location","$timeout","orderService","$ngBootbox","$state","feeService","employeeService",CarReverseCommentManagentController]);
 //过滤器
angular.module("tms").filter('numbersZero',()=>{
    return (input)=>{
        var number=null;
        if(isNaN(input)){
            return number = 0;
        }else{
            number=input;
        }
        return number;
    }
})